package com.ezhuan.users.service.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ezhuan.common.service.BaseServiceImpl;
import com.ezhuan.common.util.ConstatFinalUtil;
import com.ezhuan.common.util.PageInfoUtil;
import com.ezhuan.system.pojo.AChannel;
import com.ezhuan.system.pojo.ARegion;
import com.ezhuan.system.service.ISystemService;
import com.ezhuan.users.dao.IAIntegralHistoryDao;
import com.ezhuan.users.dao.impl.AIntegralHistoryDaoImpl;
import com.ezhuan.users.pojo.AIntegralHistory;
import com.ezhuan.users.pojo.AUsers;
import com.ezhuan.users.service.IIntegralService;
import com.ezhuan.users.service.IUsersService;

/**
 * 用户相关的Service实现类
 * 
 * @author wangshSxt
 *
 */
public class IntegralServiceImpl extends BaseServiceImpl implements IIntegralService
{
	private IAIntegralHistoryDao integralHistoryDao = new AIntegralHistoryDaoImpl();
	
	private IUsersService usersService = (IUsersService) ConstatFinalUtil.BEANMAP.get("usersService");
	private ISystemService systemService = (ISystemService) ConstatFinalUtil.BEANMAP.get("systemService");

	@Override
	public JSONObject saveOneIntegralHistoryService(AIntegralHistory integralHistory)
	{
		JSONObject resultJSON = new JSONObject();
		
		Map<String, Object> condMap = new HashMap<String,Object>();
		
		if(integralHistory.getChannelId() == Integer.valueOf(ConstatFinalUtil.CONFIG_JSON.get("channelId") + ""))
		{
			condMap.clear();
			condMap.put("usersId", integralHistory.getUsersId());
			condMap.put("channelId", Integer.valueOf(ConstatFinalUtil.CONFIG_JSON.get("channelId") + ""));
			PageInfoUtil pageInfoUtil = new PageInfoUtil();
			this.findCondListIntegralHistoryService(pageInfoUtil, condMap);
			if(pageInfoUtil.getTotalRecord() > Integer.valueOf(ConstatFinalUtil.CONFIG_JSON.get("integralMaxCount") + ""))
			{
				/* 已经超出最大次数 */
				resultJSON.put("code", "3");
				resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("3"));
				return resultJSON ; 
			}
		}
		
		/* 设置IP归属地 */
		String responseIP = this.ipLocalUtil.sendGet(integralHistory.getIp());
		JSONObject responseIPJSON = JSON.parseObject(responseIP);
		if("200".equalsIgnoreCase(responseIPJSON.getString("resultcode")))
		{
			JSONObject responseResIPJSON = responseIPJSON.getJSONObject("result");
			String area = responseResIPJSON.get("area") + "";
			String location = responseResIPJSON.get("location") + "";
			integralHistory.setIpLocation(area + ";" + location);
			
			/* 查询地区 */
			condMap.clear();
			condMap.put("name", area);
			List<ARegion> regionList = this.systemService.findCondListRegionService(new PageInfoUtil(), condMap);
			if(regionList.size() > 0 )
			{
				/* 设置地区 */
				integralHistory.setIpRegionId(regionList.get(0).getId());
			}
		}
		
		/* 保存记录 */
		boolean flag = this.integralHistoryDao.save(integralHistory);
		if(flag)
		{
			/* 自己定的汇率 */
			double rate = Double.valueOf(ConstatFinalUtil.CONFIG_JSON.get("exchangeRate") + "") ;
			if(integralHistory.getChannelId() != 0 )
			{
				/* 说明有渠道 */
				condMap.clear();
				condMap.put("id", integralHistory.getChannelId());
				AChannel channel = this.systemService.findOneChannelService(condMap);
				if(channel != null)
				{
					rate = channel.getRate() ; 
				}
			}
			
			/* 应获取的rmb */
			double moneyRMB = integralHistory.getMoney() / rate;
			
			/* 为自己增加积分 */
			condMap.clear();
			condMap.put("id", integralHistory.getUsersId());
			AUsers users = this.usersService.findOneUsersService(condMap);
			String nickName = users.getNickName() ; 
			
			users.setTotalIntegral(users.getTotalIntegral() + integralHistory.getMoney());
			users.setAccuMoney(users.getAccuMoney() + moneyRMB);
			users.setTodayMoney(users.getTodayMoney() + moneyRMB);
			users.setMoney(users.getTodayMoney() + moneyRMB);
			/* 二级学徒 */
			if(users.getSecParentId() != 0 )
			{
				users.setDrawMoney(users.getDrawMoney() + moneyRMB);
			}
			/* 一级学徒 */
			if(users.getParentId() != 0 )
			{
				users.setDrawSecMoney(users.getDrawSecMoney() + moneyRMB);
			}
			/* 更新用户信息 */
			this.usersService.updateOneUsersService(users);
			/* 二级分类 */
			int secParentId = users.getSecParentId() ; 
			
			/* 为自己的一级操作 */
			condMap.clear();
			condMap.put("id", users.getParentId());
			users = this.usersService.findOneUsersService(condMap);
			if(users != null)
			{
				users.setTotalIntegral(users.getTotalIntegral() + integralHistory.getMoney() / 2 );
				users.setAccuMoney(users.getAccuMoney() + moneyRMB);
				users.setTodayMoney(users.getTodayMoney() + moneyRMB);
				users.setMoney(users.getTodayMoney() + moneyRMB);
				this.usersService.updateOneUsersService(users);
				/* 记录明细 */
				/* 下级id */
				integralHistory.setDownUsersId(integralHistory.getUsersId());
				integralHistory.setUsersId(users.getId());
				integralHistory.setMoney(integralHistory.getMoney() / 2 );
				integralHistory.setContent(integralHistory.getContent() + ";下线做任务奖励;用户名:" + nickName);
				this.integralHistoryDao.save(integralHistory);
				
				/* 为自己的二级操作 */
				condMap.clear();
				condMap.put("id", secParentId);
				users = this.usersService.findOneUsersService(condMap);
				if(users != null)
				{
					integralHistory.setDownUsersId(integralHistory.getUsersId());
					users.setTotalIntegral(users.getTotalIntegral() + integralHistory.getMoney() / 2 );
					users.setAccuMoney(users.getAccuMoney() + moneyRMB);
					users.setTodayMoney(users.getTodayMoney() + moneyRMB);
					users.setMoney(users.getTodayMoney() + moneyRMB);
					this.usersService.updateOneUsersService(users);
					/* 记录明细 */
					integralHistory.setUsersId(users.getId());
					integralHistory.setMoney(integralHistory.getMoney() / 2 );
					integralHistory.setContent(integralHistory.getContent() + ";下线做任务奖励;用户名:" + nickName);
					this.integralHistoryDao.save(integralHistory);
				}
			}
			
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", integralHistory.getId() + "");
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
		}else
		{
			resultJSON.put("code", "3");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("3"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneIntegralHistoryService(AIntegralHistory integralHistory)
	{
		JSONObject resultJSON = new JSONObject();
		/* 保存记录 */
		boolean flag = this.integralHistoryDao.update(integralHistory);
		if(flag)
		{
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", integralHistory.getId() + "");
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
		}else
		{
			resultJSON.put("code", "3");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("3"));
		}
		return resultJSON;
	}

	@Override
	public AIntegralHistory findOneIntegralHistoryService(Map<String, Object> condMap)
	{
		return this.integralHistoryDao.findOneDao(condMap);
	}

	@Override
	public List<AIntegralHistory> findCondListIntegralHistoryService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		List<AIntegralHistory> integralHistoryList = this.integralHistoryDao.findCondListDao(pageInfoUtil, condMap);
		if("true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			Map<String, Object> extMap = new HashMap<String, Object>();
			/* 查询扩展对象 */
			for (Iterator iterator = integralHistoryList.iterator(); iterator.hasNext();)
			{
				AIntegralHistory integralHistory = (AIntegralHistory) iterator.next();
				
				/* 查询 */
				extMap.clear();
				extMap.put("id", integralHistory.getUsersId());
				AUsers usersObject = this.usersService.findOneUsersService(extMap);
				integralHistory.setUsersObject(usersObject);
				
				/* 查询 */
				extMap.clear();
				extMap.put("id", integralHistory.getDownUsersId());
				AUsers downUsersObject = this.usersService.findOneUsersService(extMap);
				integralHistory.setDownUsersObject(downUsersObject);
			}
		}
		return integralHistoryList ; 
	}
}
